package com.tjvib.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ResultReceiver;

import com.tjvib.common.Callback;
import com.tjvib.sensor.SensorData;
import com.tjvib.util.LogUtil;

/**
 * 传感器服务
 *
 * @param <T> 传感器数据类型
 */
public abstract class SensorService<T extends SensorData> extends Service {

    //////////////////////////////////////////////////////////////////////
    // Globals
    //////////////////////////////////////////////////////////////////////
    // Main UI communication protocol
    public static final int MSG_REGISTER_CLIENT = 1;
    public static final int MSG_UNREGISTER_CLIENT = 2;
    public static final int MSG_LOGGING_STARTED = 3;
    public static final int MSG_LOGGING_STOPPED = 4;
    public static final int MSG_LOGGING_ERROR = 5;
    public static final int MSG_SENSOR_CONNECTED = 6;
    public static final int MSG_SENSOR_DISCONNECTED = 7;
    public static final int MSG_SENSOR_CONNECTION_ERROR = 8;
    public static final String KEY_MESG = "MESG";

    public static boolean isRunning = false;

    LocalBinder mBinder = new LocalBinder();
    ResultReceiver uiMessageReceiver;


    public class LocalBinder extends Binder {
        public SensorService getService() {
            return SensorService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        isRunning = true;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return true;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            uiMessageReceiver = intent.getParcelableExtra("receiver");
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        disconnectAllSensor();
        isRunning = false;
    }

    public static boolean isRunning() {
        return isRunning;
    }

    abstract void connectSensor();

    abstract void connectSensor(String address, Callback callback);

    abstract void disconnectSensor();

    abstract void disconnectSensor(String address, Callback callback);

    public abstract void disconnectAllSensor();

    abstract void disconnectAllSensor(Callback callback);

    abstract int getSensorConnectionStatus();

    abstract int getSensorConnectionStatus(String address);

    abstract void setStreamFrequency(int freq);

    abstract void setStreamFrequency(String address, int freq);

    abstract int getStreamFrequency();

    abstract int getStreamFrequency(String address);

    abstract T getData();

    abstract T getData(String address);

    public abstract void resetTimestamp();

    public abstract void resetTimestamp(String address);

    public abstract void startRecording();

    public abstract void stopRecording();

    public abstract boolean isRecording();

    public abstract void discardData();

    public abstract void storeData(String name, Callback callback);

    //todo 通知栏
    //abstract void showNotification();

    //abstract void dismissNotification();

    void sendMessageToUI(String key, String value, int what) {
        Bundle bundle = new Bundle();
        bundle.putString(key, value);
        try {
            uiMessageReceiver.send(what, bundle);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
}
